package org.jeecg.modules.fund.service.impl;

import org.jeecg.modules.fund.entity.Fund;
import org.jeecg.modules.fund.entity.FundSubscriptionRate;
import org.jeecg.modules.fund.mapper.FundSubscriptionRateMapper;
import org.jeecg.modules.fund.mapper.FundMapper;
import org.jeecg.modules.fund.service.IFundService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 基金基本信息
 * @Author: jeecg-boot
 * @Date:   2024-08-23
 * @Version: V1.0
 */
@Service
public class FundServiceImpl extends ServiceImpl<FundMapper, Fund> implements IFundService {

	@Autowired
	private FundMapper fundMapper;
	@Autowired
	private FundSubscriptionRateMapper fundSubscriptionRateMapper;
	
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void saveMain(Fund fund, List<FundSubscriptionRate> fundSubscriptionRateList) {
		fund.setStockNum(fund.getTotalNum());
		fundMapper.insert(fund);
		if(fundSubscriptionRateList!=null && fundSubscriptionRateList.size()>0) {
			for(FundSubscriptionRate entity:fundSubscriptionRateList) {
				//外键设置
				entity.setFundId(fund.getId());
				fundSubscriptionRateMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void updateMain(Fund fund,List<FundSubscriptionRate> fundSubscriptionRateList) {
		fund.setStockNum(fund.getTotalNum()-fund.getBuyNum());
		fundMapper.updateById(fund);
		
		//1.先删除子表数据
		fundSubscriptionRateMapper.deleteByMainId(fund.getId());
		
		//2.子表数据重新插入
		if(fundSubscriptionRateList!=null && fundSubscriptionRateList.size()>0) {
			for(FundSubscriptionRate entity:fundSubscriptionRateList) {
				//外键设置
				entity.setFundId(fund.getId());
				fundSubscriptionRateMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delMain(String id) {
		fundSubscriptionRateMapper.deleteByMainId(id);
		fundMapper.deleteById(id);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			fundSubscriptionRateMapper.deleteByMainId(id.toString());
			fundMapper.deleteById(id);
		}
	}

	@Override
	public Fund getByIdLock(String fundId)
	{
		return this.baseMapper.getByIdLock(fundId);
	}
	
}
